package com.dingpeikeji.quickdeer.modules.officeup.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.modules.officeup.entity.TOfficeUp;
import com.dingpeikeji.quickdeer.modules.officeup.service.TOfficeUpService;

/**
 * 普通客户升级Controller
 * @author dingpei
 * @version 2016-12-08
 */
@Controller
@RequestMapping(value = "${adminPath}/officeup/tOfficeUp")
public class TOfficeUpController extends BaseController {

	@Autowired
	private TOfficeUpService tOfficeUpService;
	
	@ModelAttribute
	public TOfficeUp get(@RequestParam(required=false) String upid) {
		TOfficeUp entity = null;
		if (StringUtils.isNotBlank(upid)){
			entity = tOfficeUpService.get(upid);
		}
		if (entity == null){
			entity = new TOfficeUp();
		}
		return entity;
	}
	
	@RequiresPermissions("officeup:tOfficeUp:view")
	@RequestMapping(value = {"list", ""})
	public String list(TOfficeUp tOfficeUp, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TOfficeUp> page = tOfficeUpService.findPage(new Page<TOfficeUp>(request, response), tOfficeUp); 
		model.addAttribute("page", page);
		model.addAttribute("tOfficeUp", tOfficeUp);
		return "modules/officeup/tOfficeUpList";
	}

	@RequiresPermissions("officeup:tOfficeUp:view")
	@RequestMapping(value = "form")
	public String form(TOfficeUp tOfficeUp, Model model) {
		model.addAttribute("tOfficeUp", tOfficeUp);
		return "modules/officeup/tOfficeUpForm";
	}

	@RequiresPermissions("officeup:tOfficeUp:edit")
	@RequestMapping(value = "save")
	public String save(TOfficeUp tOfficeUp, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tOfficeUp)){
			return form(tOfficeUp, model);
		}
		tOfficeUpService.save(tOfficeUp);
		addMessage(redirectAttributes, "保存普通客户升级成功");
		return "redirect:"+Global.getAdminPath()+"/officeup/tOfficeUp/?repage";
	}
	
	@RequiresPermissions("officeup:tOfficeUp:edit")
	@RequestMapping(value = "delete")
	public String delete(TOfficeUp tOfficeUp, RedirectAttributes redirectAttributes) {
		tOfficeUpService.delete(tOfficeUp);
		addMessage(redirectAttributes, "删除普通客户升级成功");
		return "redirect:"+Global.getAdminPath()+"/officeup/tOfficeUp/?repage";
	}

}